//
//  ViewController.m
//  UI04_事件_触摸_手势
//
//  Created by Scott on 16/3/14.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "TouchView.h"
#import "MyTextField.h"
#import "ImageViewWithGesture.h"

@interface ViewController () <UITextFieldDelegate, TouchViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self createTouchView];
//    [self createGestureView];
    
//    [self createImageViewWithGesture];
//    [self createScreenEdgeGesture];
    
}


#pragma mark - 知识点1: 事件
/** 
 * UIEvent类,三种事件: 触摸, 晃动, 远程控制.
 * 本节重点: 触摸.
 * 重写UIResponder 关于触摸的三个方法
 */

#pragma mark - 知识点2: 触摸事件.
- (void)createTouchView {
    
    TouchView *viewOfRed = [[TouchView alloc] initWithFrame:CGRectMake(20, 50, 300, 60)];
    [self.view addSubview:viewOfRed];
    [viewOfRed release];
    
    viewOfRed.backgroundColor = [UIColor redColor];
    
    viewOfRed.delegate = self;
    
    
    // 输入框
    MyTextField *textFieldOfUser = [[MyTextField alloc] initWithFrame:CGRectMake(20, 120, 300, 60)];
    [self.view addSubview:textFieldOfUser];
    [textFieldOfUser release];
    
    
    textFieldOfUser.placeholder = @"请输入用户名";
    textFieldOfUser.borderStyle = 3;
    
    textFieldOfUser.backgroundColor = [UIColor cyanColor];
    
    textFieldOfUser.delegate = self;
}

/** 点击空白处, 释放键盘. */
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [super touchesEnded:touches withEvent:event];
//    
//    [self.view endEditing:YES];
//    
//    
//}




#pragma mark - TextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - TouchViewDelegate
- (void)didClicked:(TouchView *)touchView {
    
    NSLog(@"ddddd");
    
    
}

#pragma mark - 知识点3: 响应者链 
/** 
 * UIView的方法. userInteractionEnabled 用户交互.
 *
 * UIImageView, UILabel 用户交互默认是关闭.
 */

#pragma mark - 知识点4: 手势
- (void)createGestureView {
    
    UIView *viewOfGesture = [[UIView alloc] initWithFrame:CGRectMake(20, 200, 300, 50)];
    viewOfGesture.backgroundColor = [UIColor blueColor];
    [self.view addSubview:viewOfGesture];
    [viewOfGesture release];
    
    // 创建轻拍手势对象.
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    
    // 设置相关属性(API)
    tap.numberOfTouchesRequired = 1;
    tap.numberOfTapsRequired = 1;
    
    
    // 添加到相关视图上.
    [viewOfGesture addGestureRecognizer:tap];
    [tap release];
    
}

- (void)handleTap:(UITapGestureRecognizer *)tap {
    NSLog(@"%@", tap);
    NSLog(@"dddd");
}



#pragma mark - 知识点5: 自定义ImageView(带手势)
- (void)createImageViewWithGesture {
    
    ImageViewWithGesture *imageView = [[ImageViewWithGesture alloc] initWithImage:[UIImage imageNamed:@"3.jpg"]];
    
    imageView.frame = CGRectMake(0, 0, 300, 300);
    
    imageView.center = self.view.center;
    [self.view addSubview:imageView];
    [imageView release];

}

#pragma mark - 屏幕边缘手势
- (void)createScreenEdgeGesture {
    
    UIScreenEdgePanGestureRecognizer *edge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleEdge:)];
    
    [self.view addGestureRecognizer:edge];
    
    [edge release];
    
    // 设置边缘
    edge.edges = UIRectEdgeLeft;
    
}

- (void)handleEdge:(UIScreenEdgePanGestureRecognizer *)edge {
    
    NSLog(@"屏幕边缘");

}


#pragma mark - 知识点6: UIView的TransForm属性
/** 
 * TransForm属性主要设置view的 位移, 缩放, 旋转.
 * 详见 ImageViewWithGesture类.
 */







#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
